from urllib.parse import urlencode

import scrapy

from myspider.items import JingdongItem


class JingdongSpider(scrapy.Spider):
    name = 'jingdong'
    allowed_domains = ['www.jd.com']

    def start_requests(self):
        base_url = 'https://search.jd.com/Search?'
        params = {}
        for keyword in ['冰箱', '口红', '手机']:
            params['keyword'] = keyword
            for page in range(1, 6):
                params['page'] = page
                full_url = base_url + urlencode(params)
                yield scrapy.Request(url=full_url, callback=self.parse)

    def parse(self, response):
        li_list = response.css('#J_goodsList > ul > li')
        for li in li_list:
            item = JingdongItem()
            item['name'] = li.css('div.p-name.p-name-type-2 > a > em::text').extract_first()
            item['price'] = li.css('div.p-price > strong > i::text').extract_first()
            yield item
